import 'package:flutter/material.dart';
import 'package:flutterapp/Base/InitConfig.dart';
import 'package:flutterapp/Base/style/MyStyles.dart';

import 'package:flutterapp/utils/LoginManager.dart';

class PageLogin extends StatefulWidget {
  static const ROUTER_NAME = "PageLogin";

  @override
  _PageLoginState createState() => _PageLoginState();
}

class _PageLoginState extends State<PageLogin> {
  static const _PAGE_LOGIN = 0;
  static const _PAGE_REG = 1;
  static const _PAGE_RESET_PSW = 2;
  static int _pageIndex = 0;

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Container(
          decoration: BoxDecoration(
              gradient: LinearGradient(
            colors: [MyColors.PRIMARY_COLOR, Color(0xFF18BEA0)],
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
          )),
          child: IndexedStack(
            index: _pageIndex,
            alignment: AlignmentDirectional.topCenter,
            children: [ViewLogin()],
          ),
        ),
      ),
    );
  }
}

class ViewLogin extends StatefulWidget {
  @override
  _ViewLoginState createState() => _ViewLoginState();
}

class _ViewLoginState extends State<ViewLogin> {
  TextEditingController _phoneController = new TextEditingController();
  TextEditingController _pswController = new TextEditingController();

  @override
  void initState() {
    super.initState();
    _phoneController.text =
        InitConfig.sPrefs.get(LoginManager.ACCOUNT_KEY) ?? "";
    _pswController.text = InitConfig.sPrefs.get(LoginManager.PSW_KEY) ?? "";
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      // LoginManager.silenceLogin();
    });
  }

  @override
  void dispose() {
    _phoneController.dispose();
    _pswController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          Expanded(
            child: SingleChildScrollView(child: Container(
              width: double.infinity,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  SizedBox(
                    height: 40,
                  ),
                  Container(
                    child: Image(
                        image: AssetImage("images/pic_logo.png"), width: 100),
                    width: 160,
                    height: 160,
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(80))),
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  Text(
                    "纸嘟嘟消息助手",
                    style: MyTextStyle.WHITE_18,
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  ClipRRect(
                    borderRadius: BorderRadius.circular(8),
                    child: Container(
                      color: Colors.white,
                      padding: EdgeInsets.fromLTRB(20, 20, 20, 10),
                      width: 300,
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          SizedBox(
                            child: TextField(
                              decoration: InputDecoration(
                                  hintText: "请输入账号",
                                  hintStyle: TextStyle(fontSize: 12),
                                  border: OutlineInputBorder(
                                      borderSide: BorderSide(color: Color(0xffededed)),
                                      borderRadius: BorderRadius.circular(6)),
                                  fillColor: Color(0xffededed),
                                  filled: true,
                                  prefixIcon: Icon(Icons.phone_android)),
                              controller: _phoneController,
                            ),
                            height: 50,
                          ),
                          SizedBox(
                            height: 16,
                          ),
                          SizedBox(
                            child: TextField(
                              obscureText: true,
                              decoration: InputDecoration(
                                  hintText: "请输入密码",
                                  hintStyle: TextStyle(fontSize: 12),
                                  border: OutlineInputBorder(
                                      borderSide: BorderSide(color: Color(0xffededed)),
                                      borderRadius: BorderRadius.circular(6)),
                                  fillColor: Color(0xffededed),
                                  filled: true,
                                  prefixIcon: Icon(Icons.lock_open)),
                              controller: _pswController,
                            ),
                            height: 50,
                          ),
                          SizedBox(
                            height: 16,
                          ),
                          Container(
                            child: MaterialButton(
                              height: 42,
                              onPressed: () {
                                LoginManager.login(
                                    _phoneController.text, _pswController.text);
                              },
                              child: Text(
                                "登录",
                                style: MyTextStyle.WHITE_14,
                              ),
                              color: MyColors.PRIMARY_COLOR,
                            ),
                            width: double.infinity,
                          ),
                          // SizedBox(
                          //   height: 8,
                          // ),
                          // Row(
                          //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          //   children: [
                          //     FlatButton(
                          //       child: Text(
                          //         "立即注册",
                          //       ),
                          //       onPressed: () {},
                          //     ),
                          //     FlatButton(
                          //       child: Text("忘记密码"),
                          //       onPressed: () {},
                          //     )
                          //   ],
                          // )
                        ],
                      ),
                    ),
                  )
                ],
              ),
            ),),
          ),

        ],
      ),
    );
  }
}
